﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Xml.Linq;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework;
using System.IO;
using System.Net.NetworkInformation;
namespace ScannerDemo
{
    public partial class Login : PhoneApplicationPage
    {
        /// <summary>
        /// Declare Parameter
        /// </summary>
        WebClient client;
        App App = App.Current as App;
        Userlist UserData;
        string PasswordTemp;
        int QueryNumber;
        Stream stream;
        SoundEffect effect;
        public Login()
        {
            InitializeComponent();
            /// Create Object
            client = new WebClient();

            /// PLAY SOUND EFFECT
            stream = TitleContainer.OpenStream("Resource/ding.wav");
            effect = SoundEffect.FromStream(stream);
            FrameworkDispatcher.Update();
            bool InterNetConnection = NetworkInterface.GetIsNetworkAvailable();
            if (!InterNetConnection)
            {
                MessageBox.Show("No Internet Access");
            }
        }
        /// <summary>
        ///  Implement Application Animation
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textBox_GotFocus(object sender, RoutedEventArgs e)
        {
            Login1.AutoReverse = false;
            Login1.BeginTime = TimeSpan.Parse("0:0:0");
            Login1.SpeedRatio = 1.2;
            Login1.Begin();

        }

        private void passwordBox_GotFocus(object sender, RoutedEventArgs e)
        {
            Login1.AutoReverse = false;
            Login1.BeginTime = TimeSpan.Parse("0:0:0");
            Login1.SpeedRatio = 1.2;
            Login1.Begin();
        }

        private void passwordBox_LostFocus(object sender, RoutedEventArgs e)
        {
            Login2.SpeedRatio = 1.5;
            Login2.Begin();
        }

        private void textBox_LostFocus(object sender, RoutedEventArgs e)
        {
            Login2.SpeedRatio = 1.5;
            Login2.Begin();
        }
        /// <summary>
        /// Http : Request
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_Click(object sender, RoutedEventArgs e)
        {
            App.Loginname = usernameBox.Text;
            PasswordTemp = passwordBox.Password;
            string url = "http://localhost/mobile/Checkuser.php";
            //string url = "http://192.168.1.105/mobile/Checkuser.php";
            client.DownloadStringAsync(new Uri(url));
            
            client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
        }
        /// <summary>
        /// Get Data form XML and Navigate to Panoramapage.xaml
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                XElement UserElement = XElement.Parse(e.Result);
                var UserTemp =
                    from User in UserElement.Descendants("user")
                    where User.Element("username").Value == App.Loginname
                    select UserData = new Userlist
                    {
                        userId = User.Element("id").Value,
                        UserName = User.Element("username").Value,
                        PassWord = User.Element("password").Value,
                        Location = User.Element("location").Value,
                        Latitude = User.Element("latitude").Value,
                        Longtitude = User.Element("longtitude").Value,
                        Credit = User.Element("tel").Value
                    };
                QueryNumber = UserTemp.Count();

                if (QueryNumber == 1)
                {
                    
                    App.LoginId = UserData.userId;
                    App.Loginname = UserData.UserName;
                    App.LoginPassword = UserData.PassWord;
                    App.LoginLocation = UserData.Location;
                    App.LoginCredit= UserData.Credit;
                    App.Latitude = UserData.Latitude;
                    App.Longtitude = UserData.Longtitude;

                    if (App.LoginPassword == PasswordTemp)
                    {
                        NavigationService.Navigate(new Uri("/Panorama.xaml", UriKind.Relative));
                    }
                    else
                    {
                        
                        MessageBox.Show("Wrong Password or Username");
                    }
                }
                else
                {
                    
                    MessageBox.Show("Username invalid");
                }
            }
            else
            {
                
                /// Play Sound
                effect.Play();      
                MessageBox.Show("Internet Connection Problems");
            }
        }


        
    }
}